import axios from "axios";
import qs from 'qs';

const service = axios.create({
    baseURL: process.env.NODE_ENV === 'development' ? '/dev' : '',
    timeout: 100000,
    withCredentials: false,
});

service.interceptors.request.use(
    (config) => {
        // 设置请求头
    if(!config.headers["content-type"]) { // 如果没有设置请求头
        if(config.method === 'post' || config.method === 'POST') {
          config.headers["content-type"] = "application/x-www-form-urlencoded"; // post 请求
          config.data = qs.stringify(config.data); // 序列化,比如表单数据
        } else {
          config.headers["content-type"] = "application/json"; // 默认类型
        }
      }
      console.log("请求配置", config);
      return config;
    }
);

service.interceptors.response.use(
    (res) => {
        return res.data;
    },
    (error) => {
        console.error(error);
        return Promise.reject(error);
    }
);

export default service;